/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.template;

import com.vaadin.addon.touchkit.ui.NavigationBar;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window.Notification;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.processbase.touchprocess.ui.TouchProcessApplication;

/**
 *
 * @author mgubaidullin
 */
public class NavigationList extends NavigationView implements LayoutClickListener, Button.ClickListener {

    protected Table table = null;
    protected int rowCount = 0;
    protected NavigationBar toolbar = null;
    private Resource iconRefresh = new ThemeResource("images/sub_blue_rotate_ccw.png");

    public void initUI() {
        addStyleName("message-list");
        table = new Table();
        table.setImmediate(true);
        table.setSelectable(true);
        table.setMultiSelect(false);
        table.setNullSelectionAllowed(false);
        table.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
        table.setSizeFull();
        setContent(table);
        prepareToolbar();
        setToolbar(toolbar);
    }

    private void prepareToolbar() {

        toolbar = new NavigationBar();

        Button refresh = new Button();
        refresh.setIcon(iconRefresh);
        refresh.setStyleName("no-decoration");

        toolbar.setLeftComponent(refresh);

        final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm:ss");
        toolbar.setCaption("Updated " + formatter.format(Calendar.getInstance().getTime()));

        refresh.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                refreshTable();
                toolbar.setCaption("Updated "  + formatter.format(Calendar.getInstance().getTime()));
            }
        });
    }

    public Table getTable() {
        return table;
    }

    public void refreshTable() {
    }

    public void buttonClick(ClickEvent event) {
    }

    public void layoutClick(LayoutClickEvent event) {
    }

    public void showError(String description) {
        showMessage(description, Notification.TYPE_ERROR_MESSAGE);
    }

    public void showInformation(String description) {
        showMessage(description, Notification.TYPE_HUMANIZED_MESSAGE);
    }

    public void showWarning(String description) {
        showMessage(description, Notification.TYPE_WARNING_MESSAGE);
    }

    public void showImportantInformation(String description) {
        StringBuilder desc = new StringBuilder();
        if (description != null) {
            int i = 0;
            for (; description.length() > i + 50; i = i + 50) {
                desc.append("<br/> ").append(description.substring(i, i + 50));
            }
            desc.append("<br/> ").append(description.substring(i));
        } else {
            desc.append("<br/> null");
        }
        getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("informationCaption"), desc.substring(0), Notification.TYPE_ERROR_MESSAGE);

    }

    public void showMessage(String description, int type) {
        StringBuilder desc = new StringBuilder();
        if (description != null) {
            int i = 0;
            for (; description.length() > i + 50; i = i + 50) {
                desc.append("<br/> ").append(description.substring(i, i + 50));
            }
            desc.append("<br/> ").append(description.substring(i));
        } else {
            desc.append("<br/> null");
        }
        switch (type) {
            case Notification.TYPE_WARNING_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("warningCaption"), desc.substring(0), type);
                break;
            case Notification.TYPE_HUMANIZED_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("informationCaption"), desc.substring(0), type);
                break;
            case Notification.TYPE_ERROR_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("exceptionCaption"), desc.substring(0), type);
                break;
        }
    }
}
